home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl2 / examples / rasterops / collage.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  4.8 KB  |  212 lines

  1. /*
  2.  * Copyright 1995, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* collage.c
  19.  *     This program displays images at random positions in a window.
  20.  *    An option allows the images to be zoomed by a randow zoom 
  21.  *    factor. Up to MAX_IMAGES can be read.  To run, type:
  22.  *
  23.  *    collage <imageFile1.rgb> [<imageFile2.rgb>] ...
  24.  *
  25.  *    Try "collage /usr/demos/data/textures/*.rgb"
  26.  *
  27.  *        Escape key        - exit the program
  28.  *        SPACEBAR        - toggle fixed/random zoom
  29.  *
  30.  *    David Marsland, MTS, SGI Education R & D, 1993
  31.  */
  32. #include <GL/gl.h>
  33. #include <GL/glu.h>
  34. #include <GL/glut.h>
  35.  
  36. #include <math.h>
  37. #include <stdlib.h>
  38. #include <stdio.h>
  39. #include "rgbImageFile.h"
  40.  
  41. /*  Function Prototypes  */
  42.  
  43. GLvoid  initgfx( GLvoid );
  44. GLvoid  animate( GLvoid );
  45. GLvoid  visibility( int );
  46. GLvoid  drawScene( GLvoid );
  47. GLvoid  reshape( GLsizei, GLsizei );
  48. GLvoid  keyboard( GLubyte, GLint, GLint );
  49.  
  50. void  printHelp( char * );
  51.  
  52. /* Global Definitions */
  53.  
  54. #define KEY_ESC    27    /* ascii value for the escape key */
  55.  
  56. /* Global Variables */
  57.  
  58. #define     MAX_IMAGES 100
  59.  
  60. typedef struct _image {
  61.     GLuint    *image;
  62.     int    width, height;
  63. } ImageInfo;
  64.  
  65. static ImageInfo    images[MAX_IMAGES];
  66. static ImageInfo    *curImage = NULL;
  67. static GLint        imageCount;
  68.  
  69. static GLsizei        winWidth, winHeight;
  70.  
  71. static GLboolean    zoomFlag = GL_FALSE;
  72.  
  73. GLvoid
  74. main ( int argc, char *argv[] )
  75. {
  76.     GLsizei width, height;
  77.     int    i;
  78.  
  79.     glutInit( &argc, argv );
  80.  
  81.     if (argc < 2) {
  82.         fprintf(stderr, 
  83.               "usage: %s <imageFile1> [<imageFile2> ... ]\n", argv[0] );
  84.         exit (1);
  85.     }
  86.  
  87.     imageCount = argc - 1;
  88.     if (imageCount > MAX_IMAGES) imageCount = MAX_IMAGES;
  89.  
  90.     srand48( imageCount );    /* seed random number generator */
  91.  
  92.     /* read in all image files specifed as command line args */
  93.     for ( i = 0; i < imageCount; i++ ) {
  94.         images[i].image = rgbReadImageFile( argv[i+1],
  95.                     &images[i].width, &images[i].height );
  96.     }
  97.     curImage = &images[0];
  98.  
  99.     /* create a window that is 3/4 the size of the screen */
  100.  
  101.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  102.     height = glutGet( GLUT_SCREEN_HEIGHT );
  103.     winWidth = 3*width/4;
  104.     winHeight = 3*height/4;
  105.     glutInitWindowPosition( width / 8, height / 8 );
  106.     glutInitWindowSize( winWidth, winHeight );
  107.     glutInitDisplayMode( GLUT_RGBA | GLUT_SINGLE );
  108.     glutCreateWindow( argv[0] );
  109.  
  110.     initgfx();
  111.  
  112.     glutKeyboardFunc( keyboard );
  113.     glutReshapeFunc( reshape );
  114.     glutIdleFunc( animate ); 
  115.     glutVisibilityFunc( visibility ); 
  116.     glutDisplayFunc( drawScene ); 
  117.  
  118.     printHelp( argv[0] );
  119.  
  120.     glutMainLoop();
  121. }
  122.  
  123. GLvoid
  124. printHelp( char *progname )
  125. {
  126.     fprintf(stdout, "\n\n%s - creates a collage of images "
  127.         "which can be zoomed\n\n"
  128.         "Escape key        - exit the program\n"
  129.         "SPACEBAR        - toggle fixed/random zoom\n\n",
  130.         progname);
  131. }
  132.  
  133. void
  134. initgfx( void )
  135. {
  136.     glClearColor( 0.0, 0.0, 0.0, 1.0 );
  137. }
  138.  
  139. GLvoid 
  140. keyboard( GLubyte key, GLint x, GLint y )
  141. {
  142.     switch (key) {
  143.     case ' ':
  144.         zoomFlag = !zoomFlag;
  145.         if ( !zoomFlag )
  146.             glPixelZoom( 1.0, 1.0 );
  147.         glutPostRedisplay();
  148.         break;
  149.         
  150.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  151.         exit(0);
  152.     }
  153. }
  154.  
  155. GLvoid
  156. reshape( GLsizei width, GLsizei height )
  157. {
  158.     glViewport( 0, 0, width - 1, height - 1);
  159.  
  160.     winWidth = width;
  161.     winHeight = height;
  162.  
  163.     glMatrixMode( GL_PROJECTION );
  164.     glLoadIdentity();
  165.     gluOrtho2D( 0.0, (GLdouble) winWidth, 0.0, (GLdouble) winHeight );
  166.     glMatrixMode( GL_MODELVIEW );
  167.     glLoadIdentity();
  168.     glTranslatef( 0.375, 0.375, 0.0 );
  169.     glClear( GL_COLOR_BUFFER_BIT );
  170. }
  171.  
  172. GLvoid
  173. animate( GLvoid )
  174. {
  175.         /* update the current image */
  176.  
  177.     curImage = &images[(int) (imageCount * drand48())];
  178.  
  179.     /* if zooming is enabled, randomly change the zoom factor */
  180.     if ( zoomFlag ) {
  181.         /* generates zoom values in the range [ -2.0, 2.0 ) */
  182.         glPixelZoom( ( drand48() * 4.0 ) - 2.0, 
  183.                 ( drand48() * 4.0 ) - 2.0 );
  184.     }
  185.  
  186.         /* Tell GLUT to redraw the scene */
  187.         glutPostRedisplay();
  188. }
  189.         
  190. GLvoid
  191. visibility( int state )
  192. {
  193.         if (state == GLUT_VISIBLE) {
  194.                 glutIdleFunc( animate );
  195.         } else {
  196.                 glutIdleFunc( NULL );
  197.         }
  198. }
  199.  
  200. GLvoid
  201. drawScene( GLvoid )
  202. {
  203.     GLint        xPos, yPos;
  204.  
  205.     xPos = drand48()*winWidth;
  206.     yPos = drand48()*winHeight;
  207.     glRasterPos2i( xPos, yPos );
  208.     glDrawPixels( curImage->width, curImage->height,
  209.             GL_RGBA, GL_UNSIGNED_BYTE, curImage->image );
  210.     glFlush();
  211. }
  212.